package com.vay.de.dang.online.web;

import java.util.HashMap;
import java.util.Map;

import com.vay.de.dang.online.entiy.AppConfigEntity;
import com.vay.de.dang.online.util.ResponseHelper;
import com.vay.de.dang.online.util.VnContext;

public class HomeApi extends BaseApi {

    public static void queryBroadcast(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    //String url = wrapperUrl("loanSupermarket/home/getRadioBroadcast");
                    String url = wrapperUrl("TWCMFcx/Fuq/LjNOQhr");
                    String json = doGet(url);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toBroadcast(json));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    public static void queryAppConfig(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {

                    // 文件浏览前缀
                    //String url = wrapperUrl("loanSupermarket/common/file/server/url");
                    String url = wrapperUrl("GlmuWH/Ozal/HPfpERC/Mug/LIkHKk");
                    String json = doGet(url);
                    if (JsonUtil.isOk(json)) {
                        ResponseHelper.setFilePreview(JsonUtil.readData(json));
                    }

                    //url = wrapperUrl("loanSupermarket/home/appHomeConfig");
                    url = wrapperUrl("WFOtSKme/YXtnjq/lxE");
                    json = doGet(url);
                    if (JsonUtil.isOk(json)) {
                        AppConfigEntity config = JsonUtil.toAppConfig(json);
                        VnContext.instance().saveHomeConfig(config);
                        return ReqResult.ok(config);
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    // 贷超商品数据
    public static void queryHomeCommodity(ReqCallback callback) {
        execTask(new WorkerBase(callback) {
            @Override
            public ReqResult doTask() {
                try {
                    VnContext localUser = VnContext.instance();
                    //String url = wrapperUrl("loanSupermarket/home/getLoanCommodity");
                    String url = wrapperUrl("fQcNx/iOsqGNt/QQs");
                    Map<String, Object> param = new HashMap<>();
                    param.put("isTourist", localUser.isLogin() ? "1" : 0);
                    param.put("pageNo", 1);
                    param.put("pageSize", 10);
                    //param.put("sortDirection", "");
                    //param.put("sortField", "");
                    // 类型: 0 app 显示 1：推荐显示
                    param.put(" type", "0");
                    String str = JsonUtil.objectToJson(param);
                    String json = doPostByJson(url, str);
                    if (JsonUtil.isOk(json)) {
                        return ReqResult.ok(JsonUtil.toLoanCommodityList(json));
                    } else {
                        return ReqResult.error(JsonUtil.readMsg(json));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

}
